<template>
  <div>
    <el-card class="box-card">
      <div class="learn_info" slot="header">
        <div>个人信息</div>
      </div>
      <div class="cardBox">
        <div class="left">
          <div class="avatar">
            <div class="avatart_waiyuan" @click="openUpload">
              <el-avatar :size="100" :src="$store.state.user.profile.avatar" fit="cover" v-if="$store.state.user.profile.avatar"></el-avatar>
              <el-avatar :size="100" :src="circleUrl" fit="cover" v-else></el-avatar>
            </div>
          </div>
          <div class="info">
            <div v-for="(item,index) in info" :key="index">
              <el-row>
                <el-col :span="8">
                  <div class="grid-content left ep-bg-purple-dark">{{ item.name }}:</div>
                </el-col>
                <el-col :span="16">
                  <div class="grid-content right ep-bg-purple-dark">{{item.value}}</div>
                </el-col>
              </el-row>
            </div>
          </div>
        </div>
        <div class="right">
          <modifyiInformation></modifyiInformation>
        </div>
      </div>
    </el-card>
    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
      <el-upload accept="" action="" class="upload-demo" drag :http-request="upload" multiple>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过500kb</div>
      </el-upload>
    </el-dialog>
  </div>
</template>

<script>
import modifyiInformation from './modifyiInformation.vue'
import { uploadAvatar } from '@/api/user'
export default {
  components: {
    modifyiInformation
  },
  data() {
    return {
      profile: this.$store.state.user.profile,
      circleUrl: 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png',
      info: [
        { name: '用户名', value: this.$store.state.user.profile.username ? this.$store.state.user.profile.username : '还没有设置，赶紧设置吧！' },
        { name: '昵称', value: this.$store.state.user.profile.nickname ? this.$store.state.user.profile.nickname : '还没有设置，赶紧设置吧！' },
        { name: '邮箱', value: this.$store.state.user.profile.email ? this.$store.state.user.profile.email : '还没有设置，赶紧设置吧！' },
        { name: '手机号', value: this.$store.state.user.profile.phone ? this.$store.state.user.profile.phone : '还没有设置，赶紧设置吧！' },
        { name: '学校', value: this.$store.state.user.profile.school ? this.$store.state.user.profile.school : '还没有设置，赶紧设置吧！' },
        { name: '性别', value: this.$store.state.user.profile.gender ? this.$store.state.user.profile.gender : '还没有设置，赶紧设置吧！' },
        { name: '学号', value: this.$store.state.user.profile.stuNo ? this.$store.state.user.profile.stuNo : '还没有设置，赶紧设置吧！' }
      ],
      dialogVisible: false
    }
  },
  computed: {
    user() {
      return this.$store.state.user.profile
    }
  },
  watch: {
    user(newName, oldName) {
      this.info = [
        { name: '用户名', value: this.$store.state.user.profile.username ? this.$store.state.user.profile.username : '还没有设置，赶紧设置吧！' },
        { name: '昵称', value: this.$store.state.user.profile.nickname ? this.$store.state.user.profile.nickname : '还没有设置，赶紧设置吧！' },
        { name: '邮箱', value: this.$store.state.user.profile.email ? this.$store.state.user.profile.email : '还没有设置，赶紧设置吧！' },
        { name: '手机号', value: this.$store.state.user.profile.phone ? this.$store.state.user.profile.phone : '还没有设置，赶紧设置吧！' },
        { name: '学校', value: this.$store.state.user.profile.school ? this.$store.state.user.profile.school : '还没有设置，赶紧设置吧！' },
        { name: '性别', value: this.$store.state.user.profile.gender ? this.$store.state.user.profile.gender : '还没有设置，赶紧设置吧！' },
        { name: '学号', value: this.$store.state.user.profile.stuNo ? this.$store.state.user.profile.stuNo : '还没有设置，赶紧设置吧！' }
      ]
    }
  },
  methods: {
    openUpload() {
      this.dialogVisible = true
    },
    handleClose() {
      this.dialogVisible = false
    },
    upload(item) {
      let formData = new FormData()
      let file = item.file
      formData.append('file', file)
      formData.append('userId', this.profile.userId)
      uploadAvatar(formData).then(res => {
        if (res.data) {
          this.$message({
            type: 'success',
            message: res.msg
          })
          this.$store.dispatch('user/getUser')
        } else {
          this.$message.error(res.msg)
        }
      })
      this.dialogVisible = false
    }
  }
}
</script>

<style lang="less" scoped>
.cardBox {
  min-height: 400px;
  display: flex;
  gap: 10px;
  .left {
    flex: 1.5;
  }
  .right {
    flex: 3;
    background-color: #fafafa;
  }

  .avatar {
    width: 100%;
    display: flex;
    justify-content: center;
    margin-bottom: 10px;
    .avatart_waiyuan {
      width: 120px;
      height: 120px;
      border-radius: 60px;
      display: flex;
      justify-content: center;
      align-items: center;
      background-color: #f2f6fc;
    }
  }
  .info {
    width: 100%;
    padding: 7px 0;
    background-color: #fafafa;
  }
  .el-row {
    margin-bottom: 10px;
  }
  .el-row:last-child {
    margin-bottom: 0;
  }
  .el-col {
    border-radius: 4px;
    .left {
      text-align: center;
      line-height: 36px;
      color: #909399;
      font-weight: 700;
      font-size: 18px;
    }
    .right {
      text-align: center;
      line-height: 36px;
      color: #909399;
      font-size: 14px;
    }
  }

  .grid-content {
    border-radius: 4px;
    min-height: 36px;
  }
}
.Box {
  margin-top: 40px;
  background-color: #fafafa;
}
</style>
